package cn.iocoder.yudao.module.infra.convert.infocolumncollect;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.CodegenPreviewRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.CodegenUpdateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.column.CodegenColumnRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTableRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.infotablecollect.vo.InfoTCDetailRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.infotablecollect.vo.InfoTCUpdateReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.infotablecollect.vo.InfoTableCollectRespVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenColumnDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
import cn.iocoder.yudao.module.infra.dal.dataobject.infotablecollect.InfoTableCollectDO;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import org.apache.ibatis.type.JdbcType;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.infra.controller.admin.infocolumncollect.vo.*;
import cn.iocoder.yudao.module.infra.dal.dataobject.infocolumncollect.InfoColumnCollectDO;

/**
 * 基础资料表元素汇总 Convert
 *
 * @author lry
 */
@Mapper
public interface InfoColumnCollectConvert {

    InfoColumnCollectConvert INSTANCE = Mappers.getMapper(InfoColumnCollectConvert.class);


    @Mappings({
            @Mapping(source = "name", target = "tableName"),
            @Mapping(source = "comment", target = "tableComment"),
    })
    InfoTableCollectDO convert(TableInfo bean);

    List<InfoColumnCollectDO> convertList(List<TableField> list);

    @Mappings({
            @Mapping(source = "name", target = "columnName"),
            @Mapping(source = "metaInfo.jdbcType", target = "dataType", qualifiedByName = "getDataType"),
            @Mapping(source = "comment", target = "columnComment"),
            @Mapping(source = "metaInfo.nullable", target = "nullable"),
            @Mapping(source = "keyFlag", target = "primaryKey"),
            @Mapping(source = "keyIdentityFlag", target = "autoIncrement"),
            @Mapping(source = "columnType.type", target = "javaType"),
            @Mapping(source = "propertyName", target = "javaField"),
    })
    InfoColumnCollectDO convert(TableField bean);

    InfoTableCollectRespVO convert(CodegenTableDO bean);

    @Named("getDataType")
    default String getDataType(JdbcType jdbcType) {
        return jdbcType.name();
    }

    InfoColumnCollectDO convert(InfoColumnCollectCreateReqVO bean);

    InfoColumnCollectDO convert(InfoColumnCollectUpdateReqVO bean);

    InfoColumnCollectRespVO convert(InfoColumnCollectDO bean);


    PageResult<InfoColumnCollectRespVO> convertPage(PageResult<InfoColumnCollectDO> page);

    List<InfoColumnCollectRespVO> convertList02(List<InfoColumnCollectDO> list);
    CodegenTableDO convert(InfoTCUpdateReqVO.Table bean);

    InfoTableCollectRespVO convert(InfoTableCollectDO bean);

    default InfoTCDetailRespVO convert(InfoTableCollectDO table, List<InfoColumnCollectDO> columns) {
        InfoTCDetailRespVO respVO = new InfoTCDetailRespVO();
        respVO.setTable(convert(table));
        respVO.setColumns(convertList02(columns));
        return respVO;
    }

    default List<CodegenPreviewRespVO> convert(Map<String, String> codes) {
        return codes.entrySet().stream().map(entry -> {
            CodegenPreviewRespVO respVO = new CodegenPreviewRespVO();
            respVO.setFilePath(entry.getKey());
            respVO.setCode(entry.getValue());
            return respVO;
        }).collect(Collectors.toList());
    }

}
